package edu.krazy.engine;


/**
 * @(#)Camera.java
 *
 *
 * @author 
 * @version 1.00 2008/11/10
 */


public class Camera {

 float fov;
 float aspect;
 float near;
 float far;
 
 Vertex3f position;
 Vertex3f target, newTarget;
 
 
 public Camera(float fv,float asp, float n, float f)
 {
 	
 	fov = fv;
 	aspect = asp;
 	near = n;
 	far = f;
 	
 	position = new Vertex3f(-40.0f,1.8f,6f);
 	target = new Vertex3f(0f,0f,0f);
 }
 
	 public Vertex3f getPosition(){
	 	
	 	return position;
	 }
	 
	 public void setPosition(Vertex3f p){
	 	
	 	position = p;
	 }
	 
	 public void setPosition(float x, float y, float z){
	 	position = new Vertex3f(x,y,z);
	 	
	 }
	 
	 public void setPositionX(float x){
	 	
	 	position.setX(x);
	 }
	
	//metodos get
 	public float getPositionX(){
 		return	position.getX();
 	}
 	public float getPositionY(){
 		return	position.getY();
 	}
 	public float getPositionZ(){
 		return position.getZ();
 	}
 
	 
	 public void setPositionZ(float z){	
	 	position.setZ(z);
	 }   
	 	
	 public void setPositionY(float y){
	 	
	 position.setY(y);
 	}
 	
 	public void setTargetX(float x){
 		target.setX(x);
 	}
 	public void setTargetY(float y){
 		target.setY(y);
 	}
 	public void setTargetZ(float z){
 		target.setZ(z);
 	}
 	//metodos get
 	public float getTargetX(){
 		return	target.getX();
 	}
 	public float getTargetY(){
 		return	target.getY();
 	}
 	public float getTargetZ(){
 		return target.getZ();
 	}
 	
 	
 	
 	public void moveByX(float inc){
    	
	    float tmp = position.getX();
	    tmp= tmp +inc;
	    setPositionX(tmp);
	    tmp = target.getX();
	    tmp= tmp +inc;
	    target.setX(tmp); 	
    }
        public void moveByZ(float inc){
    	
    	 float tmp = position.getZ();
    	 tmp= tmp +inc;
    	 setPositionZ(tmp);
    	 tmp = target.getZ();
    	 tmp= tmp +inc;
    	target.setZ(tmp); 
    		
    	
    }
 	public void moveByLocalAxis(int dir){
 		
 		float directionVector[] = new float[2];
 		float offset = .4f;
 		switch(dir){
 			
 			case Utils.NORTH:
 				directionVector[0]=target.getX()-position.getX();
 				directionVector[1] = target.getZ()-position.getZ();
 				//normalize
 				directionVector = Utils.normalize2f(directionVector);
 				//multiplicar el unitario por la nueva magnitud
 				directionVector[0] = directionVector[0]*offset;
 				directionVector[1] = directionVector[1]*offset;
 				//sumar el nuevo vector de direccion a la posicion inicial y al target
 				position.setX(position.getX()+directionVector[0]);
 				position.setZ(position.getZ()+directionVector[1]);
 				
 				target.setX(position.getX()+directionVector[0]);
 				target.setZ(position.getZ()+directionVector[1]);
 				
 				break;
 				
 				
 			case Utils.SOUTH:
 				
 				directionVector[0]=target.getX()-position.getX();
 				directionVector[1] = target.getZ()-position.getZ();
 				//normalize
 				directionVector = Utils.normalize2f(directionVector);
 				//multiplicar el unitario por la nueva magnitud
 				directionVector[0] = directionVector[0]*offset;
 				directionVector[1] = directionVector[1]*offset;
 				//sumar el nuevo vector de direccion a la posicion inicial y al target
 				position.setX(position.getX()+-1*directionVector[0]);
 				position.setZ(position.getZ()+-1*directionVector[1]);
 				
 				target.setX(position.getX()+directionVector[0]);
 				target.setZ(position.getZ()+directionVector[1]);
 				
 				
 				break;	
 			
 			
 		}
 		
 	}
 	
 	public void followTarget(Vertex3f object)
 	{	//fix camera target
 		target.setX(object.getX());
 		target.setZ(object.getZ());
 		float distanceToTarget =4f;
 		//follow object from behind
 		float directionVector[] = new float[2];
 		directionVector[0]=object.getX()-position.getX();
 		directionVector[1]=object.getZ()-position.getZ();
 		directionVector=Utils.normalize2f(directionVector);
 		float distance = Utils.getDistance(position,object);
 		//update camera position
 	
 	
 		
 	}
  public void rotate(float angle){
    	
    
    	/*
    	 *  | x | * | cos@  sin@ | =  (x*cos@)+(z*-sin@)
    	 *  | z |   |-sin@  cos@ | =  (x*sin@)+(z*cos@)
    	 *
    	 */
    	
    	//get distance Vector
       float tmp[] = new float[2];
       float distanceVector[] = new float[2];
       float resultVector[] = new float[2];
       
       tmp[0] = target.getX()-position.getX();
       tmp[1] = target.getZ()-position.getZ();
       distanceVector = tmp;
       //rotate distance vector the given angle
       resultVector[0] = distanceVector[0]*(float)Math.cos(angle)+distanceVector[1]*-(float)Math.sin(angle);
       resultVector[1] = distanceVector[0]*(float)Math.sin(angle)+distanceVector[1]*(float)Math.cos(angle);
   
       System.out.println("Result Vector: "+resultVector[0]+" , "+resultVector[1]);
       
      
 		newTarget = new Vertex3f(resultVector[0],position.getY(),resultVector[1]);
 		
 		target = new Vertex3f(position.getX()+newTarget.getX(),position.getY(),position.getZ()+newTarget.getZ());
 		//System.out.println("Car look at: "+target.getX()+","+target.getY()+","+target.getZ());     
    	
    }
 
}